/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIData;
import com.carnation.mong.platform.domain.MOIModel;
import com.carnation.mong.platform.domain.MOIRelationType;
import com.carnation.mong.platform.service.AnalysisService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class AnalysisServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private AnalysisService service;

	public AnalysisServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(AnalysisService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("analyze".equals(method)) {
			returnStr = analyze(item);
		} else if ("getRelations".equals(method)) {
			returnStr = getRelations(item);
		} else if ("getRelationsInh".equals(method)) {
			returnStr = getRelationsInh(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String analyze(Item item) {
		if (item.getChildren().size() == 4) {// 4个参数
			MOIData data = instanceMOIData(item.getChildren().get(0));
			MOIRelationType rt = instanceMOIRelationType(item.getChildren()
					.get(1));

			int direction = getInteger(item.getChildren().get(2)
					.getLabelParts()[1]);
			boolean withChildRelationTypes = getBoolean(item.getChildren().get(
					3).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("data:" + data + ",rt:" + rt + ",direction:"
						+ direction + ",withChildRelationTypes:"
						+ withChildRelationTypes);
			}
			Object obj = null;
			try {
				obj = service.analyze(data, rt, direction,
						withChildRelationTypes);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getRelations(Item item) {
		if (item.getChildren().size() == 3) {// 3个参数
			MOIModel model = instanceMOIModel(item.getChildren().get(0));
			MOIRelationType rt = instanceMOIRelationType(item.getChildren()
					.get(1));
			int direction = getInteger(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("model:" + model + ",rt:" + rt + ",direction:"
						+ direction);
			}
			Object obj = null;
			try {
				obj = service.getRelations(model, rt, direction);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getRelationsInh(Item item) {
		if (item.getChildren().size() == 3) {// 3个参数
			MOIModel model = instanceMOIModel(item.getChildren().get(0));
			MOIRelationType rt = instanceMOIRelationType(item.getChildren()
					.get(1));
			int direction = getInteger(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("model:" + model + ",rt:" + rt + ",direction:"
						+ direction);
			}
			Object obj = null;
			try {
				obj = service.getRelationsInh(model, rt, direction);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
